package com.milk.servlet;

import com.milk.Dao.EmployeesDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class RegisterServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型为HTML，并指定字符编码为UTF-8
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        // 获取输出流对象
        PrintWriter out = response.getWriter();

        // 获取用户名和密码参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 打印调试信息
        System.out.println("Username: " + username);
        System.out.println("Password: " + password);

        // 调用EmployeesDao的方法进行注册
        int result = EmployeesDao.registerEmployee(username, password);
        if (result > 0) {
            // 注册成功，重定向到登录页面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        } else {
            out.println("<html><body>");
            out.println("<h3>注册失败，请重试！</h3>");
            out.println("</body></html>");
        }
    }
}